package com.wangsanfei.ihg.wechat.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.wangsanfei.base.BaseController;
import com.wangsanfei.ihg.wechat.model.Notice;
import com.wangsanfei.model.FileInfo;
import com.wangsanfei.model.User;
import com.wangsanfei.vo.BaseVo;

public class NoticeController extends BaseController{

	@ActionKey("/manage/notice")
	public void manage_index(){
		
		Notice vo=getModel(Notice.class);
		Record record=new Record();
		
		record.set("searchContent", getPara("searchContent"));
		Page<Record> page=Notice.dao.pagequery(getParaToInt("pageNumber"), getParaToInt("pageSize"),vo,record);
		
		setAttr("nav", "notice");
		setAttr("page", page);
		setAttr("record", record);
		
		renderJsp("/manage/notice/notice-list.jsp");
	}
	
	@ActionKey("/manage/notice/view")
	public void manage_notice_view(){
		setAttr("nav", "notice");
		renderJsp("/manage/notice/notice-view.jsp");
	}
	
	@ActionKey("/manage/notice/addDisp")
	public void manage_notice_addDisp(){
		
		setAttr("now", new Date());
		setAttr("nav", "notice");
		
		renderJsp("/manage/notice/notice-add.jsp");
	}
	
	@ActionKey("/manage/notice/add")
	public void manage_notice_add(){
		
		User currenUser=getCurrenUser();
		
		FileInfo slogo=addFile("slogo", currenUser.getStr("id"));
		FileInfo blogo=addFile("blogo", currenUser.getStr("id"));
		
		Notice vo=getModel(Notice.class);
		vo.set("creator", currenUser.getStr("id"));
		vo.set("updator", currenUser.getStr("id"));
		if(slogo!=null){
			vo.set("slogo", slogo.getStr("id"));
		}
		if(blogo!=null){
			vo.set("blogo", blogo.getStr("id"));
		}
		
		vo=Notice.dao.add(vo);
		
		setAttr("nav", "notice");
		redirect("/manage/notice");
	}
	
	@ActionKey("/manage/notice/editDisp")
	public void manage_notice_editDisp(){
		
		String id=getPara("id");
		
		Notice notice=Notice.dao.findById(id);
		
		setAttr("vo", notice);
		setAttr("nav", "notice");
		renderJsp("/manage/notice/notice-edit.jsp");
	}
	
	@ActionKey("/manage/notice/edit")
	public void manage_notice_edit(){
		
		User currenUser=getCurrenUser();
		
		FileInfo slogo=addFile("slogo", currenUser.getStr("id"));
		FileInfo blogo=addFile("blogo", currenUser.getStr("id"));
		
		Notice vo=getModel(Notice.class);
		vo.set("creator", currenUser.getStr("id"));
		vo.set("updator", currenUser.getStr("id"));
		if(slogo!=null){
			vo.set("slogo", slogo.getStr("id"));
		}
		if(blogo!=null){
			vo.set("blogo", blogo.getStr("id"));
		}
		
		Notice.dao.edit(vo);
		
		setAttr("nav", "notice");
		redirect("/manage/notice");
	}
	
	@ActionKey("/manage/notice/del")
	public void manage_notice_del(){
		
		String id=getPara("id");
		Notice notice=Notice.dao.findById(id);
		notice.set("removed", "1");
		notice.update();
		setAttr("nav", "notice");
		redirect("/manage/notice");
	}
	
	@ActionKey("/notice/all")
	public void notices(){
		BaseVo baseVo=new BaseVo();
		List<Record> notices=Notice.dao.findAll();
		
		baseVo.setData(notices);
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	@ActionKey("/notice/detail")
	public void events_detail(){
		BaseVo baseVo=new BaseVo();
		String id=getPara("id");
		Notice notice=Notice.dao.findById(id);
		notice.set("readCount", notice.getInt("readCount")+1);
		notice.update();
		
		baseVo.setData(notice);
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	@ActionKey("/notice/zan")
	public void notice_zan(){
		BaseVo baseVo=new BaseVo();
		String id=getPara("id");
		Notice notice=Notice.dao.findById(id);
		notice.set("praiseCount", notice.getInt("praiseCount")+1);
		notice.update();
		
		baseVo.setData(notice);
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
}
